package top.chukongxiang.monitorservice.javafx.fxml.components;

import javafx.scene.control.TreeCell;
import javafx.scene.control.TreeView;
import javafx.util.Callback;
import lombok.Getter;
import lombok.RequiredArgsConstructor;
import lombok.Setter;

import java.util.function.Function;

/**
 * @author 楚孔响
 * @version 1.0
 * @date 2023/10/26 18:25
 */
@RequiredArgsConstructor
public class TreeViewPropertyCellFactory<T> implements Callback<TreeView<T>, TreeCell<T>> {

    private final Function<T, String> convert;

    @Getter
    @Setter
    private boolean enableEmpty = false;

    @Override
    public TreeCell<T> call(TreeView<T> param) {
        return new TreeCell<>() {
            @Override
            protected void updateItem(T item, boolean empty) {
                super.updateItem(item, empty);
                String text = null;
                if (enableEmpty) {
                    text = convert.apply(item);
                } else if (!empty && item != null) {
                    text = convert.apply(item);
                }
                setText(text);
            }
        };
    }
}
